<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var arr = [7,2,8,1,5,6,4];
        function shares(arr){
            var max_out = 0;
            var min_in = arr[0];
            for(var i = 0;i < arr.length; i++){
                if(arr[i] < min_in){
                    min_in = arr[i];
                }
                if(arr[i] - min_in > max_out){
                    max_out = arr[i] - min_in;
                }
            }
            return max_out
        }
        console.log(shares(arr));
    </script>
</head>
<body>
    
</body>
</html>